package com.financial.transactionhistory.controller;

import com.financial.common.core.domain.R;
import com.financial.transactionhistory.domain.FinancialTransaction;
import com.financial.transactionhistory.dto.FinancialBudgetAddDto;
import com.financial.transactionhistory.dto.FinancialBudgetListDto;
import com.financial.transactionhistory.vo.FinancialBudgetVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.financial.common.annotation.Log;
import com.financial.common.core.controller.BaseController;
import com.financial.common.core.domain.AjaxResult;
import com.financial.common.enums.BusinessType;
import com.financial.transactionhistory.domain.FinancialBudget;
import com.financial.transactionhistory.service.IFinancialBudgetService;
import com.financial.common.utils.poi.ExcelUtil;
import com.financial.common.core.page.TableDataInfo;

/**
 * 预算Controller
 *
 * @author chenda
 * @date 2025-06-16
 */
@RestController
@RequestMapping("/financial/budget")
@Api(tags =  "预算相关接口")
public class FinancialBudgetController extends BaseController
{
    @Autowired
    private IFinancialBudgetService financialBudgetService;

/**
 * 查询预算列表
 */
@PreAuthorize("@ss.hasPermi('financial:budget:list')")
@GetMapping("/list")
@ApiOperation("查询预算列表")
    public TableDataInfo<List<FinancialBudgetVo>> list(FinancialBudgetListDto dto )
    {
        startPage();
        List<FinancialBudgetVo> list = financialBudgetService.selectFinancialBudgetList(dto);
        return getDataTable(list);
    }

    /**
     * 导出预算列表
     */
    @PreAuthorize("@ss.hasPermi('financial:budget:export')")
    @Log(title = "预算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出预算列表")
    public void export(HttpServletResponse response, FinancialBudgetListDto dto )
    {
        List<FinancialBudgetVo> list = financialBudgetService.selectFinancialBudgetList(dto);
        ArrayList<FinancialBudget> financialBudgetArrayList = new ArrayList<>();
        list.forEach(x->{
            FinancialBudget financialTransaction = new FinancialBudget();
            BeanUtils.copyProperties(x,financialTransaction);
        });
        ExcelUtil<FinancialBudget> util = new ExcelUtil<FinancialBudget>(FinancialBudget.class);
        util.exportExcel(response, financialBudgetArrayList, "预算数据");
    }

    /**
     * 获取预算详细信息
     */
    @PreAuthorize("@ss.hasPermi('financial:budget:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预算详细信息")
    public R<FinancialBudgetVo> getInfo(@ApiParam(value = "预算ID", required = true)
                                   @PathVariable("id") Long id)
    {
                return R.ok(financialBudgetService.selectFinancialBudgetById(id));
    }

    /**
     * 新增预算
     */
    @PreAuthorize("@ss.hasPermi('financial:budget:add')")
    @Log(title = "预算", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预算")
    public AjaxResult add(@ApiParam(value = "预算实体", required = true) @RequestBody FinancialBudgetAddDto dto)
    {
        return toAjax(financialBudgetService.insertFinancialBudget(dto));
    }

    /**
     * 修改预算
     */
    @PreAuthorize("@ss.hasPermi('financial:budget:edit')")
    @Log(title = "预算", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改预算")
    public AjaxResult edit(@ApiParam(value = "预算实体", required = true)  @RequestBody FinancialBudgetAddDto dto)
    {
        return toAjax(financialBudgetService.updateFinancialBudget(dto));
    }

    /**
     * 删除预算
     */
    @PreAuthorize("@ss.hasPermi('financial:budget:remove')")
    @Log(title = "预算", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除预算")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(financialBudgetService.deleteFinancialBudgetByIds(ids));
    }
}